<#
.SYNOPSIS
  App Icon Generator
.DESCRIPTION
  Auto-generate launcher icons from root directory
#>

# Set console encoding
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

# Find project root
$rootDir = $PWD.Path
while ($rootDir -ne "" -and -not (Test-Path "$rootDir\pubspec.yaml")) {
    $rootDir = Split-Path $rootDir -Parent
}

if (-not $rootDir) {
    Write-Host "ERROR: Not in Flutter project (missing pubspec.yaml)" -ForegroundColor Red
    exit 1
}

# Switch to root
Set-Location $rootDir

# Create default config if missing
if (-not (Test-Path "flutter_launcher_icons.yaml")) {
    @"
flutter_launcher_icons:
  image_path: "assets/images/logo.png"
  android: "launcher_icon"
  min_sdk_android: 21
  ios: true
  remove_alpha_channel_ios: true
  remove_alpha_ios: true
"@ | Out-File "flutter_launcher_icons.yaml" -Encoding UTF8
    Write-Host "NOTICE: Created default config" -ForegroundColor Yellow
}

# Generate icons
dart run flutter_launcher_icons:generate

if ($LASTEXITCODE -ne 0) {
    Write-Host "ERROR: Generation failed" -ForegroundColor Red
    Write-Host "REQUIRED CHECK:" -ForegroundColor Yellow
    Write-Host "1. Confirm assets/images/logo.png exists"
    Write-Host "2. Check YAML file format"
    exit 1
}

Write-Host "SUCCESS: App icons updated" -ForegroundColor Green